package lab3_2;

import java.util.ArrayList;

public class Exercise extends ArrayList<BinaryOperation> {
    private static final int DEFAULT_COLUMNS = 5;
    
    public Exercise() {
        super();
    }
    
    // 检查是否包含相同的算式
    public boolean contains(BinaryOperation operation) {
        for (BinaryOperation op : this) {
            if (op.equals(operation)) {
                return true;
            }
        }
        return false;
    }
    
    // 产生加法运算习题
    public void generateAdditionExercise(int operationCount) {
        this.clear();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = AdditionOperation.generateAdditionOperation();
            while (contains(anOperation)) {
                anOperation = AdditionOperation.generateAdditionOperation();
            }
            this.add(anOperation);
        }
    }
    
    // 产生减法运算习题
    public void generateSubstractExercise(int operationCount) {
        this.clear();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = SubstractOperation.generateSubstractOperation();
            while (contains(anOperation)) {
                anOperation = SubstractOperation.generateSubstractOperation();
            }
            this.add(anOperation);
        }
    }
    
    // 产生混合运算习题
    public void generateBinaryExercise(int operationCount) {
        this.clear();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = BinaryOperation.generateBinaryOperation();
            while (contains(anOperation)) {
                anOperation = BinaryOperation.generateBinaryOperation();
            }
            this.add(anOperation);
        }
    }
    
    // 格式化显示
    public void formateAndDisplay(int columnsPerRow) {
        for (int i = 0; i < this.size(); i++) {
            System.out.print((i + 1) + ". " + this.get(i) + "=\t");
            if ((i + 1) % columnsPerRow == 0) {
                System.out.println();
            }
        }
        if (this.size() % columnsPerRow != 0) {
            System.out.println();
        }
    }
    
    // 重载方法，使用默认列数
    public void formateAndDisplay() {
        formateAndDisplay(DEFAULT_COLUMNS);
    }
}